#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QFileDialog>
#include <QMessageBox>
#include <QSettings>
#include <QDebug>
#include <QDir>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}

/* // ZC: 写 ini的方式 (m_psetting 就是 QSettings实例指针)
void Config::Set(QString qstrnodename,QString qstrkeyname,QVariant qvarvalue)
{
    m_psetting->setValue(QString("/%1/%2").arg(qstrnodename).arg(qstrkeyname), qvarvalue);
}
*/
QString MainWindow::JsDir()
{
	QString strNodename = "js_file";
	QString strKeyname = "dir";
	//QSettings set(":/ini/cfg.ini", QSettings::IniFormat);// ZC: 资源文件的方式 -- 缺点:编辑起来麻烦，不灵活
	QSettings set(QCoreApplication::applicationDirPath() + "/cfg.ini", QSettings::IniFormat);// ZC: 本地文件的方式 -- 缺点:不要忘记该文件要一起复制到目标目录
    qDebug() << "QCoreApplication::applicationDirPath() -- "<< QCoreApplication::applicationDirPath();

	QVariant qvar = set.value(QString("/%1/%2").arg(strNodename).arg(strKeyname));
	QString strJsDir = qvar.toString();
	qDebug() << strJsDir;
	QDir jsDir( strJsDir );

	if (jsDir.exists())
        return strJsDir;
    return "";
}

void MainWindow::on_pbtnSelectFile_clicked()
{
    QString dir = JsDir();

    QString strFileName = QFileDialog::getOpenFileName(this,
                                                       tr("Open File"),
                                                       dir,
                                                       "",
                                                       0);
    if (!strFileName.isNull())
    {
        ui->tedtMsg->append("选择的文件为："+strFileName);
        ui->ledtJsFile->setText( strFileName );
    }
    else
        ui->tedtMsg->append("未选择文件");
}

void MainWindow::on_pbtnJsBlockIdxs_clicked()
{
    QString str = ui->ledtJsFile->text().trimmed();
    if (str.isEmpty())
    {
        QMessageBox::information(NULL, "请选择js代码文件", "未选择js代码文件", QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);
        return;
    }

    QList<int> list;
    FjsOper.JsBlockIndexs(str, list);
    ui->tedtMsg->append("js代码块的数量为："+list.count());
    ui->tedtMsg->append("js代码块的序号依次为：");
    for (int i=0; i<list.count(); i++)
        ui->tedtMsg->append("\t ["+QString::number(i)+"]: "+QString::number(list.at(i)));
}




void MainWindow::on_pbtnJsBlock_clicked()
{
    QString str = ui->ledtJsFile->text().trimmed();
    if (str.isEmpty())
    {
        QMessageBox::information(NULL, "请选择js代码文件", "未选择js代码文件", QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);
        return;
    }

    QString strIdx = ui->ledtJsBlockIdx->text().trimmed();
    if (strIdx.isEmpty())
    {
        QMessageBox::information(NULL, "请输入js代码块序号", "未输入js代码块序号", QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);
        return;
    }
    int iIdx = strIdx.toInt();
    if (iIdx <= 0)
    {
        QMessageBox::information(NULL, "输入的js代码块序号硬大于0", "输入的js代码块序号不正确:"+iIdx, QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);
        return;
    }

    QString strJsBlock;

    QList<int> list;
    FjsOper.JsBlockGetByIdx(str, iIdx, strJsBlock);
    ui->tedtMsg->append("js代码块的内容为：");
    ui->tedtMsg->append(strJsBlock);
}
